var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/baseComponent/input.tsx
var input_exports = {};
__export(input_exports, {
  Input: () => Input,
  InputNumber: () => InputNumber,
  InputNumberRange: () => InputNumberRange,
  InputTrigger: () => InputTrigger,
  Password: () => Password,
  Search: () => Search,
  TextArea: () => TextArea
});
module.exports = __toCommonJS(input_exports);
var import_icons = require("@ant-design/icons");
var import_antd = require("antd");
var import_useCursor = __toESM(require("rc-input-number/es/hooks/useCursor"));
var import_react = __toESM(require("react"));
var import_functionalComponent = require("../functionalComponent");
var import_util = require("../util");
function getInput(inputComponent, trigger = false) {
  return class extends import_util.IUPComponent {
    constructor(props) {
      super(props);
      this.parentEl = null;
      this.inputEl = null;
      this.Comp = inputComponent;
      this.lastInputTxt = "";
      this.onInputChange = (e) => {
        var _a, _b;
        const currentValue = e && e.target ? e.target.value : e;
        let value = currentValue;
        if (this.Comp !== import_antd.InputNumber && this.props.parser) {
          value = this.props.parser(value);
        }
        if (this.props.hasOwnProperty("value")) {
          this.state.value = value;
          (_b = (_a = this.props).onChange) == null ? void 0 : _b.call(_a, value);
        } else {
          this.setValue(value);
        }
        this.innerNotify([value], "onChange").then();
      };
      /**
       * 过滤多余的属性
       * @param inputProps
       * @param type input类型
       */
      this.getInputProps = (inputProps, type) => {
        inputProps.allowClear = inputProps.allowClear ?? true;
        if (this.Comp === import_antd.InputNumber) {
          delete inputProps.allowClear;
          const precision = this.getPrecision(true);
          inputProps.precision = this.getPrecision();
          inputProps.controls = false;
          if (!inputProps.formatter && !inputProps.parser) {
            if (type === "rate") {
              inputProps.formatter = (value) => {
                return import_util.iup.thousandNumber(import_util.iup.numberPrecision(value * 100), {
                  precision: import_util.iup.getCursorPosition() > -1 ? void 0 : precision
                });
              };
              inputProps.parser = (value) => {
                const v = +value.replace(/\$\s?|(,*)/g, "");
                return import_util.iup.isNumber(precision) ? +import_util.iup.numberPrecision(v / 100).toFixed(precision + 2) : import_util.iup.numberPrecision(v / 100);
              };
            } else {
              inputProps.formatter = (value) => {
                return import_util.iup.thousandNumber(value, {
                  precision: import_util.iup.getCursorPosition() > -1 ? void 0 : precision
                });
              };
              inputProps.parser = (value) => value.replace(/\$\s?|(,*)/g, "");
            }
          }
        } else if (type) {
          inputProps.type = type;
        }
        if (this.Comp !== import_antd.InputNumber) {
          delete inputProps.formatter;
          delete inputProps.parser;
        }
        if (this.Comp === import_antd.Input.Password) {
          if (inputProps.disabled) {
            inputProps.visibilityToggle = false;
          }
        }
        if (trigger) {
          delete inputProps.onTrigger;
          delete inputProps.editable;
        }
        if (inputProps.disabled) {
          delete inputProps.allowClear;
          inputProps.placeholder = "";
        }
        if (inputProps.maxLength && !inputProps.count) {
          const max = inputProps.maxLength;
          inputProps.count = {
            show: false,
            max,
            strategy: (txt) => {
              if (import_util.iup.strLen(txt) <= max) {
                this.lastInputTxt = txt;
              } else {
                let tmp = txt.substring(0, max);
                while (tmp && import_util.iup.strLen(tmp) > max) {
                  tmp = tmp.substring(0, tmp.length - 1);
                }
                this.lastInputTxt = tmp;
              }
              return import_util.iup.strLen(txt);
            },
            exceedFormatter: () => {
              return this.lastInputTxt;
            }
          };
          delete inputProps.maxLength;
        }
        return inputProps;
      };
      this.elClick = (e) => {
        var _a, _b, _c, _d;
        if (this.props.disabled) {
          return;
        }
        if (this.editable) {
          if (!import_util.iup.closest(e.target, (el) => el.classList.contains("ant-input-clear-icon")) && import_util.iup.closest(e.target, (el) => el.classList.contains("ant-input-suffix"))) {
            (_b = (_a = this.props).onTrigger) == null ? void 0 : _b.call(_a, e);
          }
        } else if (!import_util.iup.closest(e.target, (el) => el.classList.contains("ant-input-clear-icon"))) {
          (_d = (_c = this.props).onTrigger) == null ? void 0 : _d.call(_c, e);
        }
      };
      this.dbClick = (e) => {
        var _a, _b;
        if (this.props.disabled) {
          return;
        }
        (_b = (_a = this.props).onTrigger) == null ? void 0 : _b.call(_a, e);
      };
      this.getTooltipRender = (title, tooltip) => () => {
        if (import_util.iup.isBoolean(tooltip)) {
          return title;
        } else if (import_util.iup.isFunction(tooltip)) {
          return tooltip(title);
        }
        return tooltip;
      };
      this.getFormatValue = (type) => {
        return this.props.formatter && this.Comp !== import_antd.InputNumber ? this.props.formatter(this.state.value || "") : this.state.value;
      };
      this.state.value = props.defaultValue;
      if (this.props.type === "number" && this.Comp !== import_antd.InputNumber) {
        this.Comp = import_antd.InputNumber;
      }
    }
    get editable() {
      var _a;
      return ((_a = this.props) == null ? void 0 : _a.editable) ?? true;
    }
    static getDerivedStateFromProps(nextProps, prevState) {
      return import_util.IUPComponent.propsToState(nextProps, prevState, ["value"]);
    }
    setValue(value, cb) {
      if (this.state.value === value) {
        return;
      }
      this.setState({ value }, () => {
        var _a, _b;
        (_b = (_a = this.props).onChange) == null ? void 0 : _b.call(_a, value);
        cb == null ? void 0 : cb(value);
      });
    }
    getValue() {
      return this.state.value;
    }
    getPrecision(calc = false) {
      const { type, precision } = this.props;
      if (["percent", "prc", "amt", "amount", "qty", "rate"].includes(type) && import_util.iup.isNumber((0, import_util.getGlobalConfig)().default.precision[type])) {
        const p = (0, import_util.getGlobalConfig)().default.precision[type];
        if (calc && type === "rate") {
          return Math.max(0, p - 2);
        }
        return p;
      }
      if (!import_util.iup.isNullOrEmpty(precision) && ["number", "prc", "amt", "amount", "qty", "rate", "percent"].includes(type)) {
        const tmp = Number(precision);
        return import_util.iup.isNumber(tmp) ? type === "rate" && calc ? Math.max(0, tmp - 2) : tmp : void 0;
      }
      return precision;
    }
    componentDidMount() {
      var _a, _b;
      this.inputEl = ((_a = this.outRef.current) == null ? void 0 : _a.input) || this.outRef.current;
      if (trigger) {
        this.parentEl = ((_b = this.inputEl) == null ? void 0 : _b.parentElement) || this.inputEl || this.outRef.current;
        this.parentEl.addEventListener("click", this.elClick);
        if (this.editable && this.inputEl) {
          this.inputEl.addEventListener("dblclick", this.dbClick);
        }
      }
    }
    componentWillUnmount() {
      var _a, _b;
      if (trigger) {
        (_a = this.parentEl) == null ? void 0 : _a.removeEventListener("click", this.elClick);
        if (this.editable) {
          (_b = this.inputEl) == null ? void 0 : _b.removeEventListener("dblclick", this.dbClick);
        }
      }
    }
    render() {
      const Comp = this.Comp;
      const {
        value,
        onChange,
        suffix,
        style = {},
        editable,
        className,
        type = Comp === import_antd.InputNumber ? "number" : "text",
        tooltip,
        observer,
        enterButton,
        id,
        ...inputProps
      } = this.props;
      const suffixIcon = suffix || (trigger && !inputProps.disabled ? /* @__PURE__ */ import_react.default.createElement(
        import_icons.SearchOutlined,
        {
          style: {
            cursor: "pointer",
            opacity: 0.45,
            fontSize: 14
          }
        }
      ) : null);
      const formatValue = this.getFormatValue(type);
      const innerProps = this.getInputProps(inputProps, type);
      if (Comp === import_antd.Input.Search && !enterButton && !inputProps.disabled) {
        innerProps.suffix = formatValue ? /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null) : /* @__PURE__ */ import_react.default.createElement(import_icons.SearchOutlined, { style: { cursor: "pointer", fontSize: 14, opacity: 0.45 } });
        innerProps.enterButton = null;
        innerProps.className = import_util.iup.classNames(className, `iup-${type}`, "hidden-addon-icon", {
          "iup-trigger-input": trigger && editable === false
        });
      }
      const renderInput = ({ recordCursor } = {}) => /* @__PURE__ */ import_react.default.createElement(
        Comp,
        {
          ref: this.outRef,
          className: import_util.iup.classNames(className, `iup-${type}`, { "iup-trigger-input": trigger && editable === false }),
          value: formatValue,
          style: { width: "100%", ...style },
          suffix: suffixIcon,
          ...innerProps,
          onChange: (e) => {
            recordCursor == null ? void 0 : recordCursor();
            this.onInputChange(e);
          }
        }
      );
      if ([import_antd.Input].includes(Comp) && this.props.formatter) {
        return /* @__PURE__ */ import_react.default.createElement(InnerInput, { renderInput, value: formatValue, ins: this });
      }
      return tooltip ? /* @__PURE__ */ import_react.default.createElement(import_functionalComponent.Tooltip, { title: this.getTooltipRender(formatValue || innerProps.placeholder, tooltip) }, renderInput()) : renderInput();
    }
  };
}
function InnerInput({ renderInput, value, ins }) {
  var _a;
  const { placeholder, tooltip, formatter } = ins.props;
  const [recordCursor, restoreCursor] = (0, import_useCursor.default)((_a = ins.outRef.current) == null ? void 0 : _a.input, true);
  (0, import_util.useLayoutUpdateEffect)(() => {
    formatter && value && restoreCursor();
  }, [value]);
  return tooltip ? /* @__PURE__ */ import_react.default.createElement(import_functionalComponent.Tooltip, { title: ins.getTooltipRender(value || placeholder, tooltip) }, renderInput({ recordCursor })) : renderInput({ recordCursor });
}
var Input = (0, import_util.compHoc)(getInput(import_antd.Input));
var InputNumber = (0, import_util.compHoc)(getInput(import_antd.InputNumber));
var TextArea = (0, import_util.compHoc)(getInput(import_antd.Input.TextArea));
var Password = (0, import_util.compHoc)(getInput(import_antd.Input.Password));
var Search = (0, import_util.compHoc)(getInput(import_antd.Input.Search));
var InputTrigger = (0, import_util.compHoc)(
  getInput(import_antd.Input, true)
);
var InputNumberRange = (0, import_util.compHoc)(
  ({ outRef, style, placeholder, value, onChange, allowClear, ...props }) => {
    const getValueArr = () => {
      const tmp = import_util.iup.split(value);
      tmp[0] = tmp[0] ? +tmp[0] : null;
      tmp[1] = tmp[1] ? +tmp[1] : null;
      if (!import_util.iup.isNumber(tmp[0])) {
        tmp[0] = null;
      }
      if (!import_util.iup.isNumber(tmp[1])) {
        tmp[1] = null;
      }
      return tmp;
    };
    const placeholderArr = import_util.iup.split(placeholder);
    const vauleArr = getValueArr();
    const handleChange = (idx) => (v) => {
      const newValue = [vauleArr[0], vauleArr[1]];
      newValue[idx] = v;
      onChange == null ? void 0 : onChange(newValue);
    };
    (0, import_util.useExtendRef)(outRef, () => {
      return {
        getValue() {
          return getValueArr();
        },
        setValue(v) {
          onChange == null ? void 0 : onChange(v);
        }
      };
    });
    return /* @__PURE__ */ import_react.default.createElement(import_antd.Space.Compact, { block: true, style, className: "ant-input-number ant-input-number-outlined" }, /* @__PURE__ */ import_react.default.createElement(
      import_antd.InputNumber,
      {
        ...props,
        value: vauleArr[0],
        onChange: handleChange(0),
        controls: false,
        style: { flex: 1, width: 0, border: 0 },
        placeholder: placeholderArr[0]
      }
    ), /* @__PURE__ */ import_react.default.createElement(import_icons.SwapRightOutlined, null), /* @__PURE__ */ import_react.default.createElement(
      import_antd.InputNumber,
      {
        ...props,
        controls: false,
        onChange: handleChange(1),
        style: {
          flex: 1,
          width: 0,
          border: 0
        },
        value: vauleArr[1],
        placeholder: placeholderArr[1]
      }
    ));
  }
);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  Input,
  InputNumber,
  InputNumberRange,
  InputTrigger,
  Password,
  Search,
  TextArea
});
